package com.bootdo.jiashan.controller;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.utils.*;
import com.bootdo.jiashan.domain.StandardAreaDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.jiashan.domain.SaleOrderDO;
import com.bootdo.jiashan.service.SaleOrderService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 销售单
 * 
 * @author hejinen
 * @email 1992lcg@163.com
 * @date 2019-07-28 17:23:39
 */
 
@Controller
@RequestMapping("/jiashan/saleOrder")
@Slf4j
public class SaleOrderController {
	@Autowired
	private SaleOrderService saleOrderService;
	
	@GetMapping()
	@RequiresPermissions("jiashan:saleOrder:saleOrder")
	String SaleOrder(){
	    return "jiashan/saleOrder/saleOrder";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jiashan:saleOrder:saleOrder")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<SaleOrderDO> saleOrderList = saleOrderService.list(query);
		int total = saleOrderService.count(query);
		PageUtils pageUtils = new PageUtils(saleOrderList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jiashan:saleOrder:add")
	String add(){
	    return "jiashan/saleOrder/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jiashan:saleOrder:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		SaleOrderDO saleOrder = saleOrderService.get(id);
		model.addAttribute("saleOrder", saleOrder);
	    return "jiashan/saleOrder/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jiashan:saleOrder:add")
	public R save( SaleOrderDO saleOrder) throws IllegalAccessException {
		if(saleOrderService.save(saleOrder)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jiashan:saleOrder:edit")
	public R update( SaleOrderDO saleOrder,String removeIds) throws IllegalAccessException {
		saleOrderService.update(saleOrder,removeIds);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jiashan:saleOrder:remove")
	public R remove( Integer id){
		if(saleOrderService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jiashan:saleOrder:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		saleOrderService.batchRemove(ids);
		return R.ok();
	}

	@ResponseBody
	@PostMapping("/exportExcel")
	public String exportExcel(Integer saleOrderId, HttpServletRequest request, HttpServletResponse response){
		XSSFWorkbook workbook = saleOrderService.exportExcel(saleOrderId,response);
		try(OutputStream output= response.getOutputStream();
			BufferedOutputStream bufferedOutPut = new BufferedOutputStream(output);) {
			bufferedOutPut.flush();
			workbook.write(bufferedOutPut);
		} catch (Exception e) {
			log.error("exportExcel exception",e);
		}
		return null;
	}
	
}
